package com.lavegue.webaction;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.lavegue.common.Constants;
import com.lavegue.common.ListAction;
import com.lavegue.common.Page;
import com.lavegue.model.Area;
import com.lavegue.model.Company;
import com.lavegue.model.SysMsg;
import com.lavegue.model.UserInfo;
import com.lavegue.service.AreaService;
import com.lavegue.service.CompanyService;
import com.lavegue.service.SysMessageService;
import com.lavegue.service.UserInfoService;

/**
 * author: tianzhifen
 * date:2011-12-24
 * description:
 *
 */
public class SysMessageAction extends ListAction<SysMsg> {
	 private UserInfo user;
     private UserInfoService userInfoService;
     private SysMessageService sysMessageService;
     private List<UserInfo> listUser;
     private CompanyService companyService;
     //接受前台的数据
     private String sendName;//发送者名称
     private Integer receverId;//接受者Id
	 private String sendtitle;//标题
	 private String  sendContent;//内容
	 
	 private Page<SysMsg> pageSysMsgObj;
	 private SysMsg sysMsg;
	 private String sysMsgId;
	 private String actionError;
	 private AreaService areaService;
	 Page<UserInfo> pageUserObj;
	 private String usersId; //获取多个用户ID(sysMessage.jsp)
	 private Integer flagDivDisp;
	// 省列表
	private List<Area> provinces;
	// 城市列表
	private List<Area> cityList;
	// 区域列表
	private List<Area> areaList;
	
	private String accBalBegin;  //账户开始余额
	private String accBalEnd;  //账户结束余额
	
	//后台-前往发送系统消息
	 public String listSysMes() {
		 if(null == this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		   {
			   return "lg";
		   }
		//  listUser=userInfoService.findAllUserInfo();
		//获取所有的公司名称
		List<Company> comList = companyService.findByCompany();
		// 获取省列表,在前台显示省份
		provinces = areaService.findAreaByParendIdAndlvType(Area.NO_PARENT,
						Area.AreaType.PROVINCE);
		setRequestAtrribute("companyList", comList);
		if(null == user){
			try {
				//查询所有的用户数据信息
			 pageUserObj = userInfoService.findUserInfoList(pageNo, pageSize, null, null, null, null, null, null, null, null, null, null, null);
				List<UserInfo> data = pageUserObj.getData();
				for (UserInfo info : data) {
					Area p = areaService.findById(Integer.valueOf(info.getProvince()));
					info.setProvince(p.getName()); //设置省份名称
					if (null != info.getCity() && !info.getCity().equals("")) {
						Area c = areaService.findById(Integer.valueOf(info.getCity()));
						info.setCity(c.getName());	 //设置城市名称
					}
					if (null != info.getZone() && !info.getZone().equals("")) {
						Area z = areaService.findById(Integer.valueOf(info.getZone()));
						info.setZone(z.getName());	 //设置区县名称
					} 
				}
			} catch (Exception e) {
				e.printStackTrace();
			}  
		}
		
//		flagDivDisp = 1; //用来显示用户列表
		return "listSysMes";
	 }
	 
	 //后台-发生系统消息  //20120202 修改
	 public String addSysMes()
	 {
		 if(null == this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		 {
			   return "lg";
		 }
		 SysMsg sysMsg=new SysMsg();
		 sysMsg.setSender(sendName);
		 sysMsg.setSendTime(new Date());
		 sysMsg.setStatus(0);
		 sysMsg.setTitle(sendtitle);
		 sysMsg.setContent(sendContent);
		//begin 20120202
		 if(null != receverId && !receverId.equals("")) //receverId为0表示：发送给全部用户
		 {
			 try {
			 //查询所有的用户记录
				 List<UserInfo> userList = userInfoService.findAllUserInfo();
				if (null != userList ) {
					for (int i = 0; i < userList.size(); i++) {
						 UserInfo user = userList.get(i);
						 sysMsg.setUserInfo(user);
						 sysMessageService.save(sysMsg);
					 }
				}else{
					actionError = "暂时没有用户记录,不能发送消息.";
					//sysMsg.setUserInfo(null);
					//sysMessageService.save(sysMsg);
				}
			} catch (Exception e) {
				e.printStackTrace();
				actionError = "操作失败，服务器异常，请联系管理员!";
			}
			//actionError = "";
			//end 20120202 
		 }else   //发送给多用户的处理
		 {
			 try {
				 if(null != usersId && !usersId.equals("")){
					 String userId[] = usersId.split("_");
					 for (int i = 0; i < userId.length; i++) {
						String uid = userId[i];
					//	System.out.println("用户ID=>>>"+uid);
						sysMsg.setUserInfo(userInfoService.findById(Integer.valueOf(uid)));
						sysMessageService.save(sysMsg);
					 }
				 }else {
					 actionError = "暂时没有用户记录,不能发送消息.";
				}
				 
			} catch (Exception e) {
				e.printStackTrace();
				actionError = "操作失败，服务器异常，请联系管理员!";
			}
		//	actionError = ""; 
		 }
		
		 return "susses";
	 }
	 /**sysMessage.jsp
	  * 20120207 后台-查询所有用户列表
	  * @return
	  */
	 public String findAllUserInfo(){
		 if(null == this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		 {
			 return "lg";
		 }
		//获取所有的公司名称
		List<Company> comList = companyService.findByCompany();
		// 获取省列表,在前台显示省份
		provinces = areaService.findAreaByParendIdAndlvType(Area.NO_PARENT,
					Area.AreaType.PROVINCE);
		setRequestAtrribute("companyList", comList);
		
		if(null != user){
			String account = null;      //账号
			String userName = null;     //用户姓名
			BigDecimal balBegin = null;   //用户余额
			BigDecimal balEnd = null;   //用户余额
			Integer cpNameId = null;   //公司名称ID
			
			Integer userType = null;   //用户属性
			String provinceName = null;  //省份
			String cityName = null;      //城市
			String zoneName = null;      //区县
			String provinceId = null;  //省份ID
			String cityId = null;      //城市ID
			String zoneId = null;      //区县ID
			
			if(null != user.getAccount() && !user.getAccount().equals("")){ //帐号
				account = user.getAccount(); 
			}
			if(null != user.getUserName() && !user.getUserName().equals("")){ //姓名
				userName = user.getUserName();
			}
			if(null != accBalBegin && !accBalBegin.equals("")){ //开始余额
				balBegin = new BigDecimal(accBalBegin);
			}
			if(null != accBalEnd && !accBalEnd.equals("")){   //结束余额
				balEnd = new BigDecimal(accBalEnd);
			}
			if(null != user.getCompany().getId() && !user.getCompany().getId().equals("")){ //公司
				cpNameId = user.getCompany().getId();
			}
			if(null != user.getType() && !user.getType().toString().equals("")){ //用户类型
				userType = user.getType();
			}
			 //通过省份ID查询获取名称
			//设置省市区的名称ID
			if(null != user.getProvince() && !user.getProvince().equals("")){ //省份
			//	Area areaProvince = areaService.findById(Integer.valueOf(user.getProvince()));
				 provinceId = user.getProvince();
			}
			if(null != user.getCity() && !user.getCity().equals("")){ //城市
			//	Area areaCity = areaService.findById(Integer.valueOf(user.getCity()));
				cityId = user.getCity();
			}
			if(null != user.getZone() && !user.getZone().equals("")){ //区县
			//	Area areaZone = areaService.findById(Integer.valueOf(user.getZone()));
				zoneId = user.getZone();
			}
			try {
				//查询所有的用户数据信息
				pageUserObj = userInfoService.findUserInfoList(pageNo, pageSize, null, null, userName, account, cpNameId, balBegin,balEnd, userType, provinceId, cityId, zoneId);
				List<UserInfo> data = pageUserObj.getData();
				for (UserInfo info : data) {
					Area p = areaService.findById(Integer.valueOf(info.getProvince()));
					info.setProvince(p.getName()); //设置省份名称
					if (null != info.getCity() && !info.getCity().equals("")) {
						Area c = areaService.findById(Integer.valueOf(info.getCity()));
						info.setCity(c.getName());	 //设置城市名称
					}
					if (null != info.getZone() && !info.getZone().equals("")) {
						Area z = areaService.findById(Integer.valueOf(info.getZone()));
						info.setZone(z.getName());	 //设置区县名称
					} 
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			try {
				//查询所有的用户数据信息
			 pageUserObj = userInfoService.findUserInfoList(pageNo, pageSize, null, null, null, null, null, null, null, null, null, null, null);
				List<UserInfo> data = pageUserObj.getData();
				for (UserInfo info : data) {
					Area p = areaService.findById(Integer.valueOf(info.getProvince()));
					info.setProvince(p.getName()); //设置省份名称
					if (null != info.getCity() && !info.getCity().equals("")) {
						Area c = areaService.findById(Integer.valueOf(info.getCity()));
						info.setCity(c.getName());	 //设置城市名称
					}
					if (null != info.getZone() && !info.getZone().equals("")) {
						Area z = areaService.findById(Integer.valueOf(info.getZone()));
						info.setZone(z.getName());	 //设置区县名称
					} 
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
	  }
		
		flagDivDisp = 1; //用来显示用户列表

		return listSysMes();
	 }
	 
	 /**前台-
	  * ljp 20120112  显示接受者用户的所有系统消息
	  * @return
	  */
	 public String toEnterSysMsgList(){
		 if (this.getSessionUser() == null) {// 判断用户信息是否存在
				return G_LOGIN_PAGE;
		}
		 UserInfo userInfo = this.getSessionUser(); 
			
//		 UserInfo sesUser = (UserInfo) getSessionAtrribute(Constants.SESSION_USER);
//		 Integer userid = sesUser.getId();
		 
		 Integer uid = userInfo.getId();
		    try {
		    	pageSysMsgObj = sysMessageService.queryAllSysMsgList(pageNo, pageSize,uid);
			} catch (Exception e) {
				e.printStackTrace();
			}
		
		 return "showSysMsgPage";
	 }
	 
	 //前台-ljp 20120112 查看系统消息的详情(systemMessage/lookSysMsgDetail.jsp)
	 public String showSysMsgDetail(){
		 if (this.getSessionUser() == null) {// 判断用户信息是否存在
				return G_LOGIN_PAGE;
		 }
		 UserInfo sesUser = (UserInfo) getSessionAtrribute(Constants.SESSION_USER);

//		 Integer userid = sesUser.getId();
		 
		 Integer sysMsg_Id =Integer.valueOf(sysMsgId);
		 sysMsg = sysMessageService.querySysMsgById(sysMsg_Id);
		 try {
			 	if(sysMsg.getStatus().intValue() != 1){
			 		 sysMsg.setStatus(Integer.valueOf(1));//更新状态为1已读
					 sysMessageService.update(sysMsg);  // 修改系统消息的状态
		 		}
			 
		 } catch (Exception e) {
			e.printStackTrace();
			 actionError = "操作失败，服务器异常，请联系管理员!";
			 return "errorPage";
		 }
		
		 return "showSysMsgDetailPage";
	 }
	 
	 //前台-20120112 删除系统消息记录
	 public String delSysMsgRecord(){
		 if (this.getSessionUser() == null) {// 判断用户信息是否存在
				return G_LOGIN_PAGE;
		 }
		 UserInfo userInfo = this.getSessionUser(); 
			 //userInfoService.testUserByMobile(); //假数据测试
//		 setSessionAtrribute("userObject",userInfo);
			
//		 UserInfo sesUser = (UserInfo) getSessionAtrribute(Constants.SESSION_USER);
//		// 判断用户信息是否存在
//		if (null == sesUser) {
//			return "login"; // 不存在跳转到登录页面
//		}
//		 Integer userid = sesUser.getId();
		 Integer sysMsg_Id =Integer.valueOf(sysMsgId);
		 SysMsg sysMsg = sysMessageService.querySysMsgById(sysMsg_Id);
		 boolean flagMsg = sysMessageService.deleteSysMsgById(sysMsg);

		 if(flagMsg){
			 Integer uid = userInfo.getId();
			 pageSysMsgObj = sysMessageService.queryAllSysMsgList(pageNo, pageSize,uid);
		 }else{
			 actionError = "操作失败，服务器异常，请联系管理员!";
			 return "errorPage";
		 }
		
		 
		 return "toEnterSysMsgList";
	 }
	 
	 /**前台-
	  * 20120113 多选删除
	  * @return
	  */
	 public String delAllSysMsgRecord(){
		 if (this.getSessionUser() == null) {// 判断用户信息是否存在
				return G_LOGIN_PAGE;
		 }
		 try {
			 String sysMsgid[] = sysMsgId.split("_");
			 for (int i = 0; i < sysMsgid.length; i++) {
				String msgid = sysMsgid[i];
			//	System.out.println("系统消息ID=>>>"+msgid);
				SysMsg sysMsg = sysMessageService.querySysMsgById(Integer.valueOf(msgid));
				sysMessageService.deleteSysMsgById(sysMsg); //删除消息记录
			 }
		} catch (Exception e) {
			e.printStackTrace();
			actionError = "操作失败，服务器异常，请联系管理员!";
			return "errorPage";
		}
		 
		 return "toEnterSysMsgList";
	 }
	 
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	public List<UserInfo> getListUser() {
		return listUser;
	}
	public void setListUser(List<UserInfo> listUser) {
		this.listUser = listUser;
	}
	public String getSendName() {
		return sendName;
	}
	public void setSendName(String sendName) {
		this.sendName = sendName;
	}

	public Integer getReceverId() {
		return receverId;
	}

	public void setReceverId(Integer receverId) {
		this.receverId = receverId;
	}

	public String getSendtitle() {
		return sendtitle;
	}
	public void setSendtitle(String sendtitle) {
		this.sendtitle = sendtitle;
	}
	public String getSendContent() {
		return sendContent;
	}
	public void setSendContent(String sendContent) {
		this.sendContent = sendContent;
	}

	public SysMessageService getSysMessageService() {
		return sysMessageService;
	}

	public void setSysMessageService(SysMessageService sysMessageService) {
		this.sysMessageService = sysMessageService;
	}
	

	public Page<SysMsg> getPageSysMsgObj() {
		return pageSysMsgObj;
	}

	public void setPageSysMsgObj(Page<SysMsg> pageSysMsgObj) {
		this.pageSysMsgObj = pageSysMsgObj;
	}

	public SysMsg getSysMsg() {
		return sysMsg;
	}

	public void setSysMsg(SysMsg sysMsg) {
		this.sysMsg = sysMsg;
	}

	public String getSysMsgId() {
		return sysMsgId;
	}

	public void setSysMsgId(String sysMsgId) {
		this.sysMsgId = sysMsgId;
	}

	public String getActionError() {
		return actionError;
	}

	public void setActionError(String actionError) {
		this.actionError = actionError;
	}

	public AreaService getAreaService() {
		return areaService;
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}

	public Page<UserInfo> getPageUserObj() {
		return pageUserObj;
	}

	public void setPageUserObj(Page<UserInfo> pageUserObj) {
		this.pageUserObj = pageUserObj;
	}

	public String getUsersId() {
		return usersId;
	}

	public void setUsersId(String usersId) {
		this.usersId = usersId;
	}

	public Integer getFlagDivDisp() {
		return flagDivDisp;
	}

	public void setFlagDivDisp(Integer flagDivDisp) {
		this.flagDivDisp = flagDivDisp;
	}

	public UserInfo getUser() {
		return user;
	}

	public void setUser(UserInfo user) {
		this.user = user;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public List<Area> getProvinces() {
		return provinces;
	}

	public void setProvinces(List<Area> provinces) {
		this.provinces = provinces;
	}

	public List<Area> getCityList() {
		return cityList;
	}

	public void setCityList(List<Area> cityList) {
		this.cityList = cityList;
	}

	public List<Area> getAreaList() {
		return areaList;
	}

	public void setAreaList(List<Area> areaList) {
		this.areaList = areaList;
	}

	public String getAccBalBegin() {
		return accBalBegin;
	}

	public void setAccBalBegin(String accBalBegin) {
		this.accBalBegin = accBalBegin;
	}

	public String getAccBalEnd() {
		return accBalEnd;
	}

	public void setAccBalEnd(String accBalEnd) {
		this.accBalEnd = accBalEnd;
	}

	
}

